﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace WindowsFormsApplication3
{
    public partial class frmLogin : Form
    {
        string exePath = null;
        Configuration config = null;

        public frmLogin()
        {
            InitializeComponent();
            this.KeyPress += new KeyPressEventHandler(frmLogin_KeyPress);
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            exePath = Application.ExecutablePath;
            config = ConfigurationManager.OpenExeConfiguration(exePath);
            if (config.AppSettings.Settings["user"] != null)
            {
                tbUser.Text = config.AppSettings.Settings["user"].Value;
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (tbUser.Text.ToLower()  == "admin1" && tbPwd.Text.ToLower() == "admin1")
            {
                MessageBox.Show("恭喜你，登录成功！");
                config.AppSettings.Settings.Remove("user");
                config.AppSettings.Settings.Add("user", tbUser.Text);
                //ConfigurationManager.AppSettings["user"] = tbUser.Text;
                config.Save(ConfigurationSaveMode.Modified);

            }
            else
            {
                MessageBox.Show("恭喜你，登录失败，请与网管联系！");
                tbUser.Text = "";
                tbPwd.Text = "";
                tbUser.Focus();
            }
        }


        private void tbUser_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (tbUser.Text != null && tbUser.Text != "")
                {
                    tbPwd.Focus();
                }
            }
        }

        private void tbPwd_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin.Focus();
                if (tbPwd.Text != null && tbPwd.Text != "")
                {
                    btnLogin.PerformClick();
                }
            }
        }

        void frmLogin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 27)
            {
                btnClose.PerformClick();
            }
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void tbUser_TextChanged(object sender, EventArgs e)
        {
            btnLogin.Enabled = !string.IsNullOrEmpty(tbUser.Text) && !string.IsNullOrEmpty(tbPwd.Text);
        }



        //private void tbPwd_TextChanged(object sender, EventArgs e)
        //{
        //    UserPassTextChanged();
        //}

        //void UserPassTextChanged()
        //{
        //    if (!string.IsNullOrEmpty(tbUser.Text) && !string.IsNullOrEmpty(tbPwd.Text))
        //    {
        //        btnLogin.Enabled = true;
        //    }
        //    else
        //    {
        //        btnLogin.Enabled = false;
        //    }
        //}
    }
}
